package com.jfl.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.ContextStartedEvent;
import org.springframework.context.event.ContextStoppedEvent;

/**
 * 系统应用监听器
 * 
 * @author 赵炎
 * @version [V1.00, 2018年11月30日]
 * @since V1.00
 */
public class ApplicationReadyListener implements ApplicationListener<ApplicationEvent>
{
    protected final Logger logger = LoggerFactory.getLogger(getClass());
    
    @Override
    public void onApplicationEvent(ApplicationEvent event)
    {
        if ((event instanceof ApplicationEnvironmentPreparedEvent))
        {
            this.logger.info("==========初始化环境变量==============");
        }
        else if ((event instanceof ApplicationPreparedEvent))
        {
            this.logger.info("==========初始化完成==============");
        }
        else if ((event instanceof ContextRefreshedEvent))
        {
            this.logger.info("==========应用刷新==============");
        }
        else if ((event instanceof ApplicationReadyEvent))
        {
            this.logger.info("=================================");
            //  SpringBoot 2.x support
            String server = ((ApplicationReadyEvent)event).getSpringApplication().getAllSources().iterator().next().toString(); 
            //String server = ((ApplicationReadyEvent)event).getSpringApplication().getSources().iterator().next().toString();
            this.logger.info("系统[{}]启动完成!!!", server.substring(server.lastIndexOf(".") + 1));
            this.logger.info("=================================");
        }
        else if ((event instanceof ContextStartedEvent))
        {
            this.logger.info("==========应用启动==============");
        }
        else if ((event instanceof ContextStoppedEvent))
        {
            this.logger.info("==========应用停止==============");
        }
        else if ((event instanceof ContextClosedEvent))
        {
            this.logger.info("==========应用关闭==============");
        }
    }
    
}
